lib.use(['element', 'table', 'form','uploadFile','tablePlug'], function () {
    var table = layui.table;
    var layer = layui.layer;
    var form = layui.form;
    var updateForm = layui.Form('cloudSetForm');
    var ossTable;
    var openIndex;
    var uploadFile=layui.uploadFile;

    //初始表格基础参数
    table.set(common.layui.table.config());

    form.on('radio(ossTypeFilter)', function(data){
        if (data.value == "4") {
            $('.local').show();
            $('.aliyun').hide();
            $('.qiniu').hide();
            $('.qcloud').hide();
            layer.style(openIndex, { height: '200px'});
        } else if (data.value == "1") {
            $('.local').hide();
            $('.qiniu').show();
            $('.aliyun').hide();
            $('.qcloud').hide();
            layer.style(openIndex, { height: '480px'});
        } else if (data.value == "2") {
            $('.local').hide();
            $('.qiniu').hide();
            $('.aliyun').show();
            $('.qcloud').hide();
            layer.style(openIndex, { height: '500px'});
        } else if (data.value == "3") {
            $('.local').hide();
            $('.qiniu').hide();
            $('.aliyun').hide();
            $('.qcloud').show();
            layer.style(openIndex, { height: '530px'});
        }
    });

    uploadFile.render({
        id:"#upload",
        callback:function (data) {
            searchTable();
        }
    });
        ossTable = table.render({
            elem: '#ossTable'
            , url: common.util.createUrl('/sys/oss/list')
            // 对分页请求的参数：page、limit重新设定名称
            , cols: [[
                  {type:'checkbox'}
                , {type: 'numbers', title: '编号' }
                , {field: 'id', title: 'Id',width: 60}
                , {field: 'name', title: '文件名称',width: 160}
                , {field: 'url', title: '地址',  minwidth: 160}
                , {field: 'createTime', title:'上传时间', width: 160}

            ]]
            ,done: function () {
                layer.closeAll('loading');
            }
        });

        //监听单元格事件
        table.on('tool(ossTableFilter)', function(obj) {
            if (obj.event === 'edit') {
                location.href = 'configUpdate.html?id=' + obj.data.id;
            }
            if (obj.event === 'del') {
                del(obj);
            }
        });

    form.on('submit(searchFilter)', function(data){
        var param = data.field;
        searchTable(param)
        return false;
    });

    $(document).on('click','#colseBtnFilter',function(){
        layer.close(openIndex);
    });
    $(document).on('click','#del',function(){
        del(table.checkStatus('ossTable'));
    });
    $(document).on('click','#cloudSet',function(){
        cloudSetting();
    });

    form.on('submit(cloudSetFormSubmitFilter)', function (data) {
        var param = updateForm.getData();
        console.log('OSS Save param is', param);
        common.ajax.post('/sys/oss/saveConfig', param, function (resp) {
            layer.alert('保存成功', function (index) {
                layer.close(index);
                layer.close(openIndex);
            })
        })
        return false;
    });

    searchTable();

    /**
     * 查询表格
     * @param params
     */
    function searchTable(params) {
        if(!params){
            params=common.from.serializeObject('#searchFrm');
        }
        ossTable.reload({where: params })
    }

    /**
     * 删除
     * @param obj
     */
    function del(obj) {
        var postData = [];
        if(obj.data.constructor === Array){
            postData=obj.data.map(function (item) {
                return item['id'];
            });
        }else{
            postData=[obj.data.id]
        }

        layer.confirm('真的删除吗', function(index){
            console.log('Config Del param is', postData);
            common.ajax.post('/sys/oss/delete', postData, function (resp) {
                searchTable();
                layer.close(index);
            })

        });
    }


   loadFormData();

    function loadFormData() {
        common.ajax.get('/sys/oss/config', {}, function (resp) {
            updateForm.setData(resp.config);

        });
    }
    function cloudSetting() {
        openIndex=layer.open({
            type: 1,
            area: ['500px', '530px'],
            fix: false, //不固定
            shadeClose: true,
            shade:0.4,
            title: '云存储配置',
            content: $('#cloudSetFormDiv')
        });
    }

});
